/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.isen.WebApplicationSpringTemperature.bean.Temperature;
import com.isen.WebApplicationSpringTemperature.database.TemperatureHelper;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author isen
 */
@ManagedBean
@SessionScoped
public class TemperatureHibernateController {

    int startId;
    int endId;
    DataModel temperatures;
    TemperatureHelper helper;
    private int recordCount = 1000;
    private int pageSize = 10;
    private Temperature current;
    private int selectedItemIndex;

    public TemperatureHibernateController() {
        helper = new TemperatureHelper();
        startId = 1;
        endId = 10;
    }

    public Temperature getSelected() {
        if (current == null) {
            current = new Temperature();
            selectedItemIndex = -1;
        }
        return current;
    }
    
    public DataModel getTemperatures() {
        if (temperatures == null) {
            temperatures = new ListDataModel(helper.getTemperatures(startId, endId));
        }
        return temperatures;
    }
    
    void recreateModel() {
        temperatures = null;
    }
    
        public boolean isHasNextPage() {
        if (endId + pageSize <= recordCount) {
            return true;
        }
        return false;
    }

    public boolean isHasPreviousPage() {
        if (startId-pageSize > 0) {
            return true;
        }
        return false;
    }

    public String next() {
        startId = endId+1;
        endId = endId + pageSize;
        recreateModel();
        return "index";
    }

    public String previous() {
        startId = startId - pageSize;
        endId = endId - pageSize;
        recreateModel();
        return "index";
    }

    public int getPageSize() {
        return pageSize;
    }

    public String prepareView(){
        current = (Temperature) getTemperatures().getRowData();
        return "browse";
    }
    public String prepareList(){
        recreateModel();
        return "index";
    }
}
